package com.binance.mbxgateway.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.binance.master.error.GeneralCode;
import com.binance.master.models.APIRequest;
import com.binance.master.models.APIResponse;
import com.binance.mbxgateway.api.IAccountApi;
import com.binance.mbxgateway.vo.ApiKeyInfoVo;
import com.binance.mbxgateway.vo.request.ActivateAccountRequest;
import com.binance.mbxgateway.vo.request.DeleteRuleByRuleIdRequest;
import com.binance.mbxgateway.vo.request.GetAccountRequest;
import com.binance.mbxgateway.vo.request.GetApiCheckRequest;
import com.binance.mbxgateway.vo.request.GetApiInfoRequest;
import com.binance.mbxgateway.vo.request.GetBlanceLedgerRequest;
import com.binance.mbxgateway.vo.request.LockApiTradeRequest;
import com.binance.mbxgateway.vo.request.SaveApiRuleRequest;
import com.binance.mbxgateway.vo.request.SetAgentFeeRequest;
import com.binance.mbxgateway.vo.request.SetGasRequest;
import com.binance.mbxgateway.vo.request.SetTradeRequest;
import com.binance.mbxgateway.vo.request.UnLockApiTradeRequest;
import com.binance.mbxgateway.vo.response.BlanceLedgerResponse;
import com.binance.mbxgateway.vo.response.TradingAccountResponse;
import com.binance.mbxgateway.web.service.IAccountService;

@RestController
public class AccountController implements IAccountApi {

    @Autowired
    private IAccountService accountService;

    @Override
    public APIResponse<List<BlanceLedgerResponse>> getBlanceLedger(
            @RequestBody @Validated APIRequest<GetBlanceLedgerRequest> request) throws Exception {
        return APIResponse.getOKJsonResult(this.accountService.getBlanceLedger(request.getBody().getUserId(),
                request.getBody().getAsset(), request.getBody().getStartTime(), request.getBody().getEndTime()));
    }

    @Override
    public APIResponse<Void> unlockApiTrade(@RequestBody @Validated APIRequest<UnLockApiTradeRequest> request)
            throws Exception {
        this.accountService.unlockApiTrade(request.getBody().getUserId());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> lockApiTrade(@RequestBody @Validated APIRequest<LockApiTradeRequest> request)
            throws Exception {
        this.accountService.lockApiTrade(request.getBody().getUserId());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<ApiKeyInfoVo> getApiCheck(@RequestBody @Validated APIRequest<GetApiCheckRequest> request)
            throws Exception {
        return APIResponse.getOKJsonResult(this.accountService.getApiCheck(request.getBody().getApkKey(),
                request.getBody().getUserId(), request.getBody().getIp(), request.getBody().getPayload(),
                request.getBody().getRecvWindow(), request.getBody().getTimestamp(), request.getBody().getSignature()));
    }

    @Override
    public APIResponse<Void> setTrade(@RequestBody @Validated APIRequest<SetTradeRequest> request) throws Exception {
        this.accountService.setTrade(request.getBody().getUserId(), request.getBody().getCanTrade());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> setGas(@RequestBody @Validated APIRequest<SetGasRequest> request) throws Exception {
        this.accountService.setGas(request.getBody().getUserId(), request.getBody().getIsUseBnbFee());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<TradingAccountResponse> getAccount(@RequestBody @Validated APIRequest<GetAccountRequest> request)
            throws Exception {
        TradingAccountResponse tradingAccountVo = this.accountService.getAccount(request.getBody().getUserId());
        return APIResponse.getOKJsonResult(tradingAccountVo);
    }

    @Override
    public APIResponse<Void> activateAccount(@RequestBody @Validated APIRequest<ActivateAccountRequest> request)
            throws Exception {
        this.accountService.activateAccount(request.getBody().getUserId(), request.getBody().getCanTrade(),
                request.getBody().getCanWithdraw(), request.getBody().getCanDeposit());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> setAgentFee(@RequestBody @Validated APIRequest<SetAgentFeeRequest> request)
            throws Exception {
        try {
            this.accountService.setAgentFee(request.getBody().getUserId(), request.getBody().getRatio());
        } catch (Exception e) {
            return APIResponse.getErrorJsonResult(APIResponse.Type.GENERAL, GeneralCode.SYS_ERROR.getCode(),
                    "commission.update_failed");
        }
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> saveApiRule(@RequestBody @Validated APIRequest<SaveApiRuleRequest> request)
            throws Exception {
        this.accountService.saveApiRule(request.getBody().getUserId(), request.getBody().getKeyId(),
                request.getBody().getIp());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> deleteRuleByRuleId(@RequestBody @Validated APIRequest<DeleteRuleByRuleIdRequest> request)
            throws Exception {
        this.accountService.deleteRuleByRuleId(request.getBody().getUserId(), request.getBody().getKeyId(),
                request.getBody().getRuleId());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<List<ApiKeyInfoVo>> getApiInfo(@RequestBody @Validated APIRequest<GetApiInfoRequest> request)
            throws Exception {
        return APIResponse.getOKJsonResult(this.accountService.getApiInfo(request.getBody().getUserId()));
    }

}
